package com.qj.yantaishan.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.qj.yantaishan.common.annotation.Where;
import com.qj.yantaishan.common.enums.WhereEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Field;
import java.util.List;

/**
 * 动态自定义查询条件工具类
 */
public class WhereUtil {


    /**
     * 拼接查询条件
     *
     * @param queryWrapper 条件对象
     * @param obj          数据实体
     * @return void 返回参数说明
     * @exception/throws
     */
    public static void convertQuery(QueryWrapper queryWrapper, Object obj) throws IllegalAccessException {
        Class clazz = obj.getClass();
        // 反射遍历属性
        for (Field field : clazz.getDeclaredFields()) {
            // 抑制Java对修饰符的检查
            field.setAccessible(true);
            // 获取属性值
            Object fieldValue =  field.get(obj);
            // 如果是null的跳过
            if (ObjectUtils.isEmpty(fieldValue)) {
                continue;
            }
            // 判断是不是空字符串
            if (fieldValue instanceof String) {
                if (StringUtils.isEmpty(fieldValue.toString().trim())) {
                    continue;
                }
            }

            // 查询注解
            Where whereAnnotation = AnnotationUtils.getAnnotation(field, Where.class);
            if(ObjectUtils.isEmpty(whereAnnotation)){
                continue;
            }
            // 获取字段名
            String fieldName = whereAnnotation.field();
            // 获取枚举
            WhereEnum whereEnum = whereAnnotation.whereEnum();
            // 拼接查询条件
            switch (whereEnum) {
                case EQ:
                    queryWrapper.eq(fieldName, fieldValue);
                    break;
                case NE:
                    queryWrapper.ne(fieldName, fieldValue);
                    break;
                case LIKE:
                    queryWrapper.like(fieldName, fieldValue);
                    break;
                case NOT_LIKE:
                    queryWrapper.notLike(fieldName, fieldValue);
                    break;
                case GT:
                    queryWrapper.gt(fieldName, fieldValue);
                    break;
                case GE:
                    queryWrapper.ge(fieldName, fieldValue);
                    break;
                case LT:
                    queryWrapper.lt(fieldName, fieldValue);
                    break;
                case LE:
                    queryWrapper.le(fieldName, fieldValue);
                    break;
                case LIKE_LEFT:
                    queryWrapper.likeLeft(fieldName, fieldValue);
                    break;
                case LIKE_RIGHT:
                    queryWrapper.likeRight(fieldName, fieldValue);
                    break;
                case IS_NULL:
                    queryWrapper.isNull(fieldValue);
                    break;
                case IS_NOT_NULL:
                    queryWrapper.isNotNull(fieldValue);
                    break;
                case IN:
                    queryWrapper.in(fieldName, (List)fieldValue);
                    break;
                case NOT_IN:
                    queryWrapper.notIn(fieldName, (List)fieldValue);
                    break;
                case ORDER_BY_ASE:
                    queryWrapper.orderByAsc(fieldValue);
                    break;
                case ORDER_BY_DESC:
                    queryWrapper.orderByDesc(fieldValue);
                    break;
                case SELECT:
                    if (fieldValue instanceof String) {
                        queryWrapper.select(((String) fieldValue).split(","));
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
